package com.jcsz.gulimall.seckill.controller;

import com.jcsz.common.to.mq.SeckillOrderTo;
import com.jcsz.common.utils.R;
import com.jcsz.gulimall.seckill.service.SeckillService;
import com.jcsz.gulimall.seckill.to.SeckillSkuRedisTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @className: SeckillController
 * @author: 一把Po吉他
 * @date: 2022/11/18 13:56
 * @version: 1.0
 **/
@Controller
public class SeckillController {

    @Autowired
    SeckillService seckillService;

    @GetMapping("getCurrentSeckillSkus")
    @ResponseBody
    public R getCurrentSeckillSkus() {
       // try { TimeUnit.MILLISECONDS.sleep(300);} catch (InterruptedException e) {e.printStackTrace();}

        List<SeckillSkuRedisTo> seckillSkuRedisTos = seckillService.getCurrentSeckillSkus();
        //获取到当前可以参加秒杀商品的信息
        return R.ok().setData(seckillSkuRedisTos);
    }

    /**
     * 根据skuId查询商品是否参加秒杀活动
     *
     * @param skuId
     * @return
     */
    @GetMapping(value = "/sku/seckill/{skuId}")
    @ResponseBody
    public R getSkuSeckilInfo(@PathVariable("skuId") Long skuId) {
        SeckillSkuRedisTo to = seckillService.getSkuSeckilInfo(skuId);
        return R.ok().setData(to);
    }

    //killId=" + killId + "&key=" + code + "&num=" + num;
    @GetMapping(value = "kill")
    public String kill(@RequestParam("killId") String killId, @RequestParam("key") String code, @RequestParam("num") Integer num, Model model) {
        String orderSn = seckillService.kill(killId,code,num);
        model.addAttribute("orderSn",orderSn);
        return "success";
    }
}
